﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WXYW.BLL;
using WXYW.DAL;

namespace WXYW
{
    public partial class UpgradeDistributeProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialPage();
            }
        }

        private void InitialPage()
        {
            UpgradeService service = new UpgradeService();
            UpgradeForm upForm = service.GetProductDetailForm(long.Parse(Request.QueryString[Constants.QUERYSTR_UPGRADE_ID]));
            productTypeLabel.Text = upForm.ApplicationProduct.ProductType.ProductTypeName;
            serialNumberLabel.Text = upForm.ApplicationProduct.ProductSerialNumber;
            problemTypeLabel.Text = upForm.ApplicationProduct.ProblemType.Name;
            decriptionLabel.Text = upForm.ApplicationProduct.ProductProblemDescription;
            appearanceLabel.Text = upForm.ApplicationProduct.ProductAppearance;
            repairTypeLabel.Text = upForm.ApplicationProduct.ProductRepairType;
            //
            UserService userservice = new UserService();
            engineerDropDownList.DataSource = userservice.GetUsersByType((int)RoleType.Engineer);
            engineerDropDownList.DataTextField = "ContactPerson";
            engineerDropDownList.DataValueField = "UserID";
            engineerDropDownList.DataBind();

            //
            distributeTimeLabel.Text = DateTime.Now.ToString("yyyy-MM-dd");
        }

        protected void saveLinkButton_Click(object sender, EventArgs e)
        {
            string changeLogContent = string.Format("");
            ChangeLogService changeLogService = new ChangeLogService();
            try
            {
                UpgradeService service = new UpgradeService();
                UpgradeForm upForm = service.GetProductDetailForm(long.Parse(Request.QueryString[Constants.QUERYSTR_UPGRADE_ID]));


                if (!upForm.ApplicationProductReference.IsLoaded)
                {
                    upForm.ApplicationProductReference.Load();
                }             
                
                upForm.DistributeTime = DateTime.Now;
                upForm.Status = "升级-待修";
                upForm.ApplicationProduct.Status = "升级-待修";
                changeLogContent = string.Format("升级产品状态改变: ApplicationProduct {0}->{1}", upForm.ApplicationProduct.Status, "升级-待修");
                changeLogService.AddChangeLog(ChangeLogType.ApplicationProduct, upForm.ApplicationProduct.ApplicationProductID.ToString(), changeLogContent);

                UserService userservice = new UserService();
                upForm.ApplicationProduct.User = userservice.GetUserByID(long.Parse(engineerDropDownList.SelectedValue));
                service.Update(upForm);

                ClientScript.RegisterStartupScript(this.GetType(), "tip", "alert('派单成功');location.href='UpgradeNeedDistributeProduct.aspx';", true);
            }
            catch (Exception ex)
            {
                LogUtility.LogUtilityIntance.LogException(ex, "UpgradeDistributeProduct.aspx::saveLinkButton_Click");
                throw ex;
            }
        }
    }
}
